package com.grq.mis.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    public static final String ERROR = "result/500.html";
    public static final String PERMISSION = "result/403.html";
    public static final String NO = "result/404.html";
    public static final String LOGIN = "/login";
    public static final String SUCCESS = "result/success.html";

    /**
     * 未登录
     *
     * @return
     */
    @ExceptionHandler(NotLoginException.class)
    public RedirectView notLoginException(NotLoginException e, HttpServletRequest request) {
        // log.error("NotLoginException:[{}]",e.getMessage(),e);
        return new RedirectView(request.getContextPath() + LOGIN);
    }

    /**
     * 404
     *
     * @return
     */
    @ExceptionHandler(NoHandlerFoundException.class)
    public ModelAndView noHandlerFoundException(NoHandlerFoundException e) {
        log.error("NoHandlerFoundException:[{}]",e.getMessage(),e);
        return new ModelAndView(NO);
    }

    /**
     * 业务
     */
    @ExceptionHandler(BusinessException.class)
    public ModelAndView fhtException(BusinessException e) {
        log.error("FhtException:[{}]", e.getMessage(), e);
        ModelAndView mv = new ModelAndView();
        mv.addObject("code", e.getCode());
        mv.addObject("msg", e.getMsg());
        mv.setViewName(ERROR);
        return mv;
    }

    /**
     * 无权限
     *
     * @return
     */
    @ExceptionHandler(NotPermissionException.class)
    public ModelAndView notPermissionException(NotPermissionException e) {
        log.error("NotPermissionException:[{}]", e.getMessage(), e);
        return new ModelAndView(PERMISSION);
    }

    /**
     * 无角色
     *
     * @return
     */
    @ExceptionHandler(NotRoleException.class)
    public ResultData notRoleException(NotRoleException e, HttpServletResponse response) {

        log.error("NotRoleException:[{}]", e.getMessage(), e);

        return new ResultData(ResultEnum.NO_ROLE, e.getMessage());
    }

    /**
     * 处理通用异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ModelAndView exception(Exception e) {

        log.error("Exception:[{}]", e.getMessage(), e);

        ModelAndView mv = new ModelAndView();
        mv.addObject("code", ResultEnum.ERROR.getCode());
        mv.addObject("msg", StringUtils.isEmpty(e.getMessage()) ? ResultEnum.ERROR.getMsg() : e.getMessage());
        mv.setViewName(ERROR);

        return mv;
    }

}
